package com.taotao.utils;

import java.math.BigDecimal;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;

public class StringUtilEx {

	public static String replaceBlank(String str) {
		String dest = "";
		if (str != null) {
			Pattern p = Pattern.compile("\\s*|\t|\r|\n");
			Matcher m = p.matcher(str);
			dest = m.replaceAll("");
		}
		return dest;
	}

	/**
	 * 获取上传文件的名字
	 * 
	 * @param fileName
	 * @return
	 */
	public static String getUploadFileName(String fileName) {
		String dest = "";
		if (StringUtils.isNotBlank(fileName)) {
			Pattern p = Pattern.compile("(.fq|.fa|.fq.gz|.fastq|.fastq.gz)$");
			Matcher m = p.matcher(fileName);
			dest = m.replaceAll("");
		}
		return dest;
	}

	/**
	 * byte(字节)根据长度转成kb(千字节)和mb(兆字节)
	 * 
	 * @param size
	 * @return
	 */
	public static String getFileSize(long bytes) {
		BigDecimal filesize = new BigDecimal(bytes);
		BigDecimal megabyte = new BigDecimal(1024 * 1024);
		float returnValue = filesize.divide(megabyte, 2, BigDecimal.ROUND_DOWN)
				.floatValue();
		if (returnValue > 1)
			return (returnValue + "MB");
		BigDecimal kilobyte = new BigDecimal(1024);
		returnValue = filesize.divide(kilobyte, 2, BigDecimal.ROUND_DOWN)
				.floatValue();
		return (returnValue + "KB");
	}

	/**
	 * 获取UUID
	 * 
	 * @return
	 */
	public static String getUUID() {
		return UUID.randomUUID().toString().replace("-", "");
	}

}
